#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# (c) Copyright 2010 - BidiX@BidiX.info
#
#
'''
    mail - 
    
    Description :
 
     Usage :
     
'''
from google.appengine.api import mail
from google.appengine.ext import webapp

from bidix.html.page import Page
from bidix.logger import log, Level

class MailReport(mail.EmailMessage):
    
    def __init__(self, **kw):
        self.sender = "app@bidix.info"
        self.to = "bidix@bidix.info"
        self.subject = "Report from app.bidix.info"
        mail.EmailMessage.__init__(self, **kw)
        

class SendReport(webapp.RequestHandler):
    def get(self):
        report = MailReport()
        report.body = """
Test de message.
"""
        report.send()
        l=log(type='report', level=Level.INFO, creator='bidix.mail', url=report.to, source=report.sender, id=report.subject)
        self.response.out.write("Report sent to %s"%report.to)

class Mailer(Page):
    """Provides an API to EmailMessage"""
    
    def initialize_page(self):
        self.with_html_editor = True
        
    
    def format_main(self):
        main = """
<form method="post" action="/mail?action=send">
    <table>
        <tr>
            <td>From: </td>
            <td><input class="text" name="sender" size="50" value=%(sender)s></td>
        </tr>
        <tr>
            <td>Reply-to: </td>
            <td><input class="text" name="reply_to" size="50" value=%(reply_to)s></td>
        </tr>
        <tr>
            <td>To: </td>
            <td><input class="text" name="to" size="50" value=%(to)s></td>
        </tr>
        <tr>
            <td>Subject : </td>
            <td><input class="text" name="subject" size="50" value=%(subject)s></td>
        </tr>
        <tr>
            <td>Body : </td>
            <td><textarea class="text" name="body">%(body)s</textarea></td>
        </tr>
        <tr>
            <td>HTML : </td>
            <td><textarea id="tinymce" name="html">%(html)s</textarea></td>
        </tr>
        <tr>
            <td colspan="2"><center><input type="submit" value="send"></center></td> 
        </tr>
    </table>
    
</form>
"""%{'path': self.request.path, 
     'sender': "",
     'reply_to':"",
     'to': "",
     'subject': "",
    'body': "",
    'html': ""
     }
        return main
    
    def post(self):
        email = mail.EmailMessage()
        email.sender = self.request.get('sender')
        email.reply_to = self.request.get('reply_to')
        email.to = self.request.get('to')
        email.subject = self.request.get('subject')
        if self.request.get('body'):
            email.body = self.request.get('body')
        if self.request.get('html'):
            email.html = self.request.get('html')
        email.send()
        l=log(type='mail', level=Level.INFO, creator='bidix.mail', url=email.to, source=email.sender, id=email.subject)
        self.out.write("Mail sent to %s"%email.to)
        return
        
        